package com.wyp168.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wyp168.gulimall.product.entity.CategoryEntity;
import com.wyp168.gulimall.product.service.CategoryService;
import com.wyp168.common.utils.R;



/**
 * 商品三级分类
 *
 * @author wyp168
 * @email 2721255240@qq.com
 * @date 2022-10-22 12:09:09
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R listWithTree(@RequestParam Map<String, Object> params){
        //获取产品菜单的树形结构
        List<CategoryEntity> entities = categoryService.listWithTree();

        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @RequestMapping(method = RequestMethod.GET, value = "/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		return categoryService.saveIfLevelLowerTree(category) ? R.ok("添加菜单成功") : R.error("添加菜单失败");
    }

    /**
     * 修改
     */
    @RequestMapping(method = RequestMethod.POST, value = "/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);

        return R.ok();
    }

    /**
     * 批量修改
     */
    @RequestMapping(method = RequestMethod.POST, value = "/updateBatch")
    //@RequiresPermissions("product:category:update")
    public R updateSortAndLevel(@RequestBody CategoryEntity[] category){
        boolean isUpdate = categoryService.updateBatchById(Arrays.asList(category));
        return isUpdate ? R.ok() : R.error("批量修改失败");

    }


    /**
     * 删除
     */
    @RequestMapping(method = RequestMethod.POST, value = "/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
		return categoryService.removeMenuByIds(Arrays.asList(catIds)) ? R.ok("删除成功") : R.error("删除失败");
    }

}
